import React, { useState, useEffect } from 'react'
import "./index.scss"
import { Flex, Typography, Tabs, Button, Table, Empty, Descriptions } from 'antd'
import { FullscreenExitOutlined, FullscreenOutlined } from "@ant-design/icons"
import { useSearchParams } from 'react-router-dom'
import RelationalGraph from '@/components/RelationalGraph'
import { portraitfxxxAPI } from '@/apis'
import { toggleFull } from '@/utils'
import FirmLogo from '@/components/FirmLogo'


const { Paragraph, Title } = Typography
const Legal = ({ name = '' }) => {
    return (
        <Flex className='legal' align="center">
            <FirmLogo name={name.slice(0, 1)} size={44} font={20}></FirmLogo>
            <div>
                <h6>{name}</h6>
                {/* <Link style={{ color: '#1c1f23' }}>TA有1家企业 &gt;</Link> */}
            </div>
        </Flex>
    )
}
const tagsTab = [
    {
        key: '0',
        label: <p>全部标签 <span>12</span></p>
    },
    {
        key: '1',
        label: <p>业务标签 <span>12</span></p>
    },
    {
        key: '2',
        label: <p>属性标签 <span>12</span></p>
    },
    {
        key: '3',
        label: <p>风险标签 <span>12</span></p>
    },
]
const infoTabs = [
    {
        key: '0',
        label: '股东情况'
    },
    {
        key: '1',
        label: '经营风险'
    },
    {
        key: '2',
        label: '司法风险'
    }
]
const jyfxTabs = [
    {
        key: 'jyfx_gscg',
        label: '公示催告',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center",
            },
            {
                title: '票号',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '票据类型',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '申请人',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '票面金额',
                dataIndex: 'C5',
                key: 'C5',
                align: 'center',
                render: (dom, rowData, index) => {
                    return rowData.C5?rowData.C5.toLocaleString():rowData.C5
                }
            },
            {
                title: '公告日期',
                dataIndex: 'C6',
                key: 'C6',
            },
        ]
    },
    {
        key: 'jyfx_gqcz',
        label: '股权出质',
        columns: [
            {
                title: '股权出质设立登记日期',
                dataIndex: 'C1',
                key: 'C1'
            },
            {
                title: '登记编号',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '出质人',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '质权人',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '处置股权数额',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '状态',
                dataIndex: 'C6',
                key: 'C6'
            }
        ]
    },
    {
        key: 'jyfx_dwdb',
        label: '对外担保',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '公告日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '担保方',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '被担保方',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '担保方式',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '担保金额',
                dataIndex: 'C6',
                key: 'C6'
            }
        ]
    },
    {
        key: 'jyfx_sxxx',
        label: '失信信息',
        columns: [
            {
                title: '法定代表人',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '执行法院',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '生效法律文书确定的义务',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '失信被执行人行为具体情形',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '案号',
                dataIndex: 'C5',
                key: 'C5',
            },
            {
                title: '涉案金额',
                dataIndex: 'C6',
                key: 'C6'
            },
            {
                title: '被执行人的履行情况',
                dataIndex: 'C7',
                key: 'C7'
            },
            {
                title: '做出执行依据单位',
                dataIndex: 'C8',
                key: 'C8'
            }
        ]
    },
    {
        key: 'jyfx_gqzy',
        label: '股权质押',
        columns: [
            {
                title: '质押冻结类型',
                dataIndex: 'C1',
                key: 'C1',
            },
            {
                title: '质押/冻结起始日',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '质押/冻结截止日',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '实际解除日期',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '实施质押冻结机构',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '涉及金额',
                dataIndex: 'C6',
                key: 'C6'
            },
            {
                title: '质押/冻结股数',
                dataIndex: 'C7',
                key: 'C7'
            },
            {
                title: '剩余未解押数',
                dataIndex: 'C8',
                key: 'C8'
            },
            {
                title: '累计质押股数',
                dataIndex: 'C9',
                key: 'C9'
            },
            {
                title: '质押/冻结期限描述',
                dataIndex: 'C10',
                key: 'C10'
            },
            {
                title: '备注',
                dataIndex: 'C11',
                key: 'C11'
            },
            {
                title: '质押/冻结事由',
                dataIndex: 'C12',
                key: 'C12'
            }
        ]
    },
    {
        key: 'jyfx_dcdy',
        label: '动产抵押',
        columns: [
            {
                title: '登记日期',
                dataIndex: 'C1',
                key: 'C1'
            },
            {
                title: '登记编号',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '登记机关',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '被担保债权类型',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '被担保债权数额',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '债务期限',
                dataIndex: 'C6',
                key: 'C6'
            },
            {
                title: '担保范围',
                dataIndex: 'C7',
                key: 'C7'
            },
            {
                title: '状态',
                dataIndex: 'C8',
                key: 'C8'
            }
        ]
    },
    {
        key: 'jyfx_jyyc',
        label: '经营异常',
        columns: [
            {
                title: '列入经营异常做出决定机关',
                dataIndex: 'C1',
                key: 'C1',
            },
            {
                title: '列入日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '移出经营异常名录原因',
                dataIndex: 'C3',
                key: 'C3'
            },
            {
                title: '移出日期',
                dataIndex: 'C4',
                key: 'C4'
            },
            {
                title: '移出经营异常作出决定机关',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '列入经营异常名录原因',
                dataIndex: 'C11',
                key: 'C11'
            }
        ]
    }
]
const sffxTabs = [
    {
        key: 'sffx_sfxz',
        label: '司法协助',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '被执行人',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '股权数额',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '执行通知文号',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '典型状态',
                dataIndex: 'C5',
                key: 'C5'
            }
        ]
    },
    {
        key: 'sffx_ktgg',
        label: '开庭公告',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '开庭日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '案由',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '案号',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '原告/上诉人',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '被告/被上诉人',
                dataIndex: 'C6',
                key: 'C6'
            },
            {
                title: '法庭',
                dataIndex: 'C7',
                key: 'C7'
            }
        ]
    },
    {
        key: 'sffx_qsgg',
        label: '欠税公告',
        columns: [
            {
                title: '发布时间',
                dataIndex: 'C1',
                key: 'C1'
            },
            {
                title: '纳税人识别号',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '欠税税种',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '欠税余额',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '负责人姓名',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '当前新发生欠税',
                dataIndex: 'C6',
                key: 'C6'
            },
            {
                title: '税务机关',
                dataIndex: 'C7',
                key: 'C7'
            }
        ]
    },
    {
        key: 'sffx_flss',
        label: '法律诉讼',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '案号',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '类型号',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '案件名称',
                dataIndex: 'C5',
                key: 'C5'
            }
        ]
    },
    {
        key: 'sffx_fygg',
        label: '法院公告',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '公告日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '上诉方',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '被诉方',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '公告类型',
                dataIndex: 'C5',
                key: 'C5'
            }
        ]
    },
    {
        key: 'sffx_hbcf',
        label: '环保处罚',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '处罚日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '决定文书号',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '处罚事由',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '处罚结果',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '处罚单位',
                dataIndex: 'C6',
                key: 'C6'
            }
        ]
    },
    {
        key: 'sffx_laxx',
        label: '立案信息',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '立案日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '案号',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '案件身份',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '法院',
                dataIndex: 'C5',
                key: 'C5'
            }
        ]
    },
    {
        key: 'sffx_zbaj',
        label: '终本案件',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '立案日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '案号',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '执行标的',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '未履行金额',
                dataIndex: 'C5',
                key: 'C5',
                render: (dom, rowData, index) => {
                    return rowData.C5?rowData.C5.toLocaleString():rowData.C5
                }
            },
            {
                title: '执行法院',
                dataIndex: 'C6',
                key: 'C6'
            },
            {
                title: '终本日期',
                dataIndex: 'C7',
                key: 'C7'
            }
        ]
    },
    {
        key: 'sffx_xzcf',
        label: '行政处罚',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '处罚日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '决定文书号',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '处罚事由/违法行为类型',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '处罚结果/内容',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '处罚单位',
                dataIndex: 'C6',
                key: 'C6'
            },
        ]
    },
    {
        key: 'sffx_bzxrxx',
        label: '被执行人信息',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '立案时间期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '执行标的',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '案号份',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '执行法院',
                dataIndex: 'C5',
                key: 'C5'
            }
        ]
    },
    {
        key: 'sffx_sdgg',
        label: '送达公告',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '发布日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '公告名称',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '案号',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '案由',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '案件身份',
                dataIndex: 'C6',
                key: 'C6'
            }
        ]
    },
    {
        key: 'sffx_xzxfl',
        label: '限制消费令',
        columns: [
            {
                title: '序号',
                dataIndex: 'C1',
                key: 'C1',
                align: "center"
            },
            {
                title: '立案日期',
                dataIndex: 'C2',
                key: 'C2',
            },
            {
                title: '案号',
                dataIndex: 'C3',
                key: 'C3',
            },
            {
                title: '姓名',
                dataIndex: 'C4',
                key: 'C4',
            },
            {
                title: '企业信息',
                dataIndex: 'C5',
                key: 'C5'
            },
            {
                title: '申请人',
                dataIndex: 'C6',
                key: 'C6'
            },
            {
                title: '发布时间',
                dataIndex: 'C7',
                key: 'C7'
            }
        ]
    }
]

export default function Jibenxinxi({ data }) {
    const [isFullScreen, setIsFullScreen] = useState(false);
    const [chartBoxHeight, setChartBoxHeight] = useState(360)
    const [fxtabkey, setfxtabkey] = useState('0')
    const [jysftabkey, setjysftabkey] = useState('')
    const [params] = useSearchParams()
    const company = params.get('company')
    const [jysfcolumns, setjysfcolumns] = useState([])
    const [jysfSource, setjysfSource] = useState([])
    useEffect(() => {
        const handleKeyDown = () => {
            // 全屏
            if (document.fullscreenElement) {
                setIsFullScreen(true);
                setChartBoxHeight('100%')
            } else {
                setIsFullScreen(false);
                setChartBoxHeight(360)
            }
        };
        document.addEventListener('fullscreenchange', handleKeyDown);
    }, [])
    const [gdxx, setGdxx] = useState({ data: [], link: [] })
    useEffect(() => {
        if (data.company_executives[0]) {
            const gsmc = data.company_executives[0].GSMC
            let gdxxdata = [gsmc]
            let gdxxlink = []
            for (let i = 0; i < data.company_executives.length; i++) {
                if (gdxxdata.indexOf(data.company_executives[i].NAME) !== -1) {
                    gdxxlink[gdxxdata.indexOf(data.company_executives[i].NAME) - 1] = gdxxlink[gdxxdata.indexOf(data.company_executives[i].NAME) - 1] + ',' + data.company_executives[i].POS
                } else {
                    gdxxdata.push(data.company_executives[i].NAME)
                    gdxxlink.push(data.company_executives[i].POS)
                }
            }
            setGdxx({
                data: gdxxdata.map(item => item.length > 8 ? item.substring(0, item.length / 2) + '\n' + item.substring(item.length / 2, item.length) : item),
                link: gdxxlink.map(item => item.length > 8 ? item.substring(0, item.length / 2) + '\n' + item.substring(item.length / 2, item.length) : item)
            })
        }
    }, [data])
    const handlefxtab = (key) => {
        setfxtabkey(key)
        if (key === '1') {
            setjysftabkey('jyfx_gscg')
            setjysfcolumns(jyfxTabs[0].columns)
        } else if (key === '2') {
            setjysftabkey('sffx_sfxz')
            setjysfcolumns(sffxTabs[0].columns)
        }
    }
    const handlejysftab = (key) => {
        setjysftabkey(key)
        if (fxtabkey === '1') {
            setjysfcolumns(jyfxTabs.filter(item => item.key === key)[0].columns)
        } else if (fxtabkey === '2') {
            setjysfcolumns(sffxTabs.filter(item => item.key === key)[0].columns)
        }
    }
    const [tableload,settableload]=useState(false)
    useEffect(() => {
        function getfxdata() {
            settableload(true)
            portraitfxxxAPI({
                company: company,
                type: jysftabkey
            }).then(res => {
                settableload(false)
                console.log(res.data)
                setjysfSource(res.data)
            })
        }
        fxtabkey!=='0' && getfxdata()
    }, [company,fxtabkey, jysftabkey])
    return (
        <div className='basic-container'>
            <Descriptions labelStyle={{
                whiteSpace: 'nowrap',
                backgroundColor: '#F7FAFF'
            }} column={2} size="small" bordered items={[
                {
                    key: '0',
                    label: '公司名称',
                    children: data.company_info[0].GSMC,
                },
                {
                    key: '1',
                    label: '统一社会信用代码',
                    children: <Paragraph copyable>{data.company_info[0].TYSHXYDM}</Paragraph>,
                },
                {
                    key: '2',
                    label: '法定代表人',
                    children: <Legal name={data.company_info[0].FDDBR} />
                },
                {
                    key: '3',
                    label: '存续状态',
                    children: data.company_info[0].DJZT
                },
                {
                    key: '4',
                    label: '成立时间',
                    children: data.company_info[0].CLRQ?.slice(0,10)
                },
                {
                    key: '5',
                    label: '核准日期',
                    children: data.company_info[0].HZRQ?.slice(0,10)
                },
                {
                    key: '6',
                    label: '注册资本',
                    children: data.company_info[0].ZZZB
                },
                {
                    key: '7',
                    label: '联系电话',
                    children: data.company_info[0].LXDH
                },
                {
                    key: '8',
                    label: '是否成员单位',
                    children: data.company_info[0].IS_CYDW === 1 ? '是' : '否'
                },
                {
                    key: '9',
                    label: '邮箱',
                    children: data.company_info[0].YX
                },
                {
                    key: '10',
                    label: '公司地址',
                    children: data.company_info[0].ZCDZ,
                    span: 2
                },
                {
                    key: '11',
                    label: '经营范围',
                    children: <Paragraph ellipsis={{
                        rows: 1,
                        expandable: 'collapsible',
                    }}>{data.company_info[0].JYFW}</Paragraph>,
                    span: 2
                }
            ]} />
            <Tabs className="tags-tab" indicator={{
                size: 20,
            }} defaultActiveKey="0" items={infoTabs} onChange={handlefxtab} />
            {fxtabkey !== '0' && <Tabs className="tags-tab" activeKey={jysftabkey} type="card" items={fxtabkey === '1' ? jyfxTabs : sffxTabs} onChange={handlejysftab} />}
            <div className="tags-container tags-container1 ">
                {fxtabkey === '0' && <> <Flex className="title" justify="space-between" align="center">
                    <p>{data.company_info[0] && data.company_info[0].GSMC}</p>
                    <Button onClick={() => toggleFull('.tags-container1')} size="small" color="primary"
                        variant="text" icon={isFullScreen ? <FullscreenExitOutlined /> : <FullscreenOutlined />}>{isFullScreen ? '退出全屏' : '全屏查看'}</Button>
                </Flex>
                    <Flex vertical justify="center" className="chart-box" style={{ width: '100%', height: chartBoxHeight }}>
                        {gdxx.data.length > 0 ? <RelationalGraph data={gdxx} /> : <Empty description='暂无数据' image={Empty.PRESENTED_IMAGE_SIMPLE}></Empty>}
                    </Flex></>}
                {fxtabkey !== '0' && <Table scroll={{x:'100%'}} loading={tableload} pagination={false} size='small' bordered={false} columns={jysfcolumns} dataSource={jysfSource}></Table>}
            </div>
        </div>
    )
}
